﻿using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using UpperComputer.Core.Entities;
using YFurion.Application;
using YFurion.Application.EFCore.Extensions;
using YFurion.Extensions;

namespace UpperComputer.Application
{
    public class BaseRoleService : EFCoreApplicationService<BaseRole, BaseRoleResultDto, long, BaseRoleResultRequestDto, BaseRoleDto>, IBaseRoleService
    {
        #region 新增角色
        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="input"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public override async Task<BaseRoleResultDto> Post(BaseRoleDto input, CancellationToken cancellationToken = default)
        {
            BaseRole baseRole = await MasterRepository.AsQueryable(br => br.Code.Equals(input.Code), ignoreQueryFilters: true).FirstOrDefaultAsync(cancellationToken);
            if (baseRole != null && !baseRole.IsDeleted)
            {
                throw Oh("角色编码已存在");
            }

            int count = await MasterRepository.CountAsync(u => u.DisplayName.Equals(input.DisplayName), cancellationToken: cancellationToken);
            if (count > 0)
            {
                throw Oh("角色名称已存在");
            }

            BaseRoleResultDto baseMenuResultDto = null;
            if (baseRole == null)
            {
                baseMenuResultDto = await base.Post(input, cancellationToken);
            }
            else
            {
                input.Id = baseRole.Id;
                baseRole.IsDeleted = false;
                baseMenuResultDto = await UpdateAsync(input, baseRole, cancellationToken);
            }
            return baseMenuResultDto;
        }
        #endregion

        #region 修改角色
        protected override async Task OnUpdateExecuting(BaseRole entity, CancellationToken cancellationToken)
        {
            await base.OnUpdateExecuting(entity, cancellationToken);
            BaseRole otherBaseRole = await MasterRepository.FirstOrDefaultAsync(br => br.Code.Equals(entity.Code) && !br.Id.Equals(entity.Id), cancellationToken: cancellationToken);
            if (otherBaseRole != null)
            {
                throw Oh("角色编码已存在");
            }
            otherBaseRole = await MasterRepository.FirstOrDefaultAsync(br => br.DisplayName.Equals(entity.DisplayName) && !br.Id.Equals(entity.Id), cancellationToken: cancellationToken);
            if (otherBaseRole != null)
            {
                throw Oh("角色名称已存在");
            }
        }

        #endregion

        #region 查询角色
        /// <summary>
        /// 查询角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        protected override IQueryable<BaseRole> CreateFilteredQuery(BaseRoleResultRequestDto input)
        {
            IQueryable<BaseRole> query = base.CreateFilteredQuery(input)
                .WhereIf(!input.Code.IsNullOrWhiteSpace(), br => br.Code.StartsWith(input.Code))
                .WhereIf(!input.DisplayName.IsNullOrWhiteSpace(), br => br.DisplayName.StartsWith(input.DisplayName));
            return query;
        }
        #endregion       
    }
}
